<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <link rel="stylesheet" href="lib/bootstrap.min.css">
    <script src="lib/jquery-1.7.2.js"></script>
    <script src="lib/bootstrap.js"></script>
    <script src="vue.js"></script>
    <title>vue 留言板</title>
    <script>
        window.onload=function () {
            new Vue({
                el:"#box",
                data:{
                    mydata:[

                ],
                    username:'',
                    age:'',
                    nowIndex:-100,
                    descrip:''
                },
                methods:{
                 add:function () {
                     this.mydata.push({
                         name:this.username,
                         age:this.age
                     });
                     this.username='';
                     this.age='';
                 },
                 deleteMsg:function (n) {
                     if(n==-2){
                         this.mydata='';



                     }else{
                         this.mydata.splice(n,1);


                     }

                 }
                }
            })
        }
    </script>
</head>
<body>
<div class="container" id="box">
    <form role="form">
        <div class="form-group">
            <lable for="username">用户名:</lable>
            <input type="text" id="username" class="form-control" placeholder="请输入用户名" v-model="username">
        </div>
        <div class="form-group">
            <lable for="age">年 龄:</lable>
            <input type="text" id="age" class="form-control" placeholder="请输入年龄" v-model="age">
        </div>
       <div class="form-group">
           <input type="button" value="添加" class="btn btn-primary" @click="add()">
           <input type="reset" value="重置" class="btn btn-danger">

       </div>
    </form>
    <hr>
    <table class="table table-bordered table-hover">
        <caption class="h2 text-info">用户信息表</caption>
        <tr class="text-danger">
            <th class="text-center">序号</th>
            <th class="text-center">名字</th>
            <th class="text-center">年龄</th>
            <th class="text-center">操作</th>
        </tr>
        <tr class="text-center" v-for="item in mydata">
            <td>{{$index}}</td>
            <td>{{item.name}}</td>
            <td>{{item.age}}</td>
            <td>
               <button class="btn btn-primary btn-sm" data-toggle="modal" data-target="#layer"  @click="nowindex=$index,descrip='确认删除这条数据吗'">删除</button>
            </td>

        </tr>
<tr v-show="mydata.length!=0">
    <td colspan="4" class="text-right">
        <button class="btn btn-danger btn-sm"  data-toggle="modal" data-target="#layer" @click="nowindex=-2,descrip='确认删除全部数据吗'">删除全部</button>
    </td>
  </tr>
        <tr v-show="mydata.length==0">
            <td colspan="4" class="text-center text-muted">
                <p>暂无数据....</p>
            </td>
        </tr>
    </table>


    <!--  模态框  -->
    <div role="dialoge" class="modal fade" id="layer">
       <div class="modal-dialog">

           <div class="modal-content">
               <div class="modal-header">
                   <button type="button" class="close" data-dismiss="modal">
                       <span>&times</span>
                   </button>
                   <h4 class="modal-title">{{descrip}}</h4>
               </div>
               <div class="modal-body text-right">
                   <button class="btn btn-primary btn-sm" data-dismiss="modal">取消</button>
                   <button class="btn btn-danger btn-sm" data-dismiss="modal" @click="deleteMsg(nowindex)">确认</button>
               </div>
           </div>
       </div>
    </div>
</div>
</body>
</html>